﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Reflection;

namespace GIVisualEditor.TaskModel
{
    public class EnvionmentVariables
    {
        public static Dictionary<string, string> oSpecialVariableMap = new Dictionary<string, string>();

        static EnvionmentVariables()
        {
            string sThisAssemblyFolder = Assembly.GetAssembly(typeof( ExecuteProcessTask )).Location;
            sThisAssemblyFolder = sThisAssemblyFolder.Substring(0, sThisAssemblyFolder.LastIndexOf(@"\") + 1);

            oSpecialVariableMap.Add("$GIFolder", sThisAssemblyFolder);
            oSpecialVariableMap.Add("$ApplicationData", Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData));
            oSpecialVariableMap.Add("$CommonApplicationData", Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData));
            oSpecialVariableMap.Add("$LocalApplicationData", Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData));
            oSpecialVariableMap.Add("$Cookies", Environment.GetFolderPath(Environment.SpecialFolder.Cookies));
            oSpecialVariableMap.Add("$Desktop", Environment.GetFolderPath(Environment.SpecialFolder.Desktop));
            oSpecialVariableMap.Add("$Favorites", Environment.GetFolderPath(Environment.SpecialFolder.Favorites));
            oSpecialVariableMap.Add("$History", Environment.GetFolderPath(Environment.SpecialFolder.History));
            oSpecialVariableMap.Add("$InternetCache", Environment.GetFolderPath(Environment.SpecialFolder.InternetCache));
            oSpecialVariableMap.Add("$Programs", Environment.GetFolderPath(Environment.SpecialFolder.Programs));
            oSpecialVariableMap.Add("$MyComputer", Environment.GetFolderPath(Environment.SpecialFolder.MyComputer));
            oSpecialVariableMap.Add("$MyMusic", Environment.GetFolderPath(Environment.SpecialFolder.MyMusic));
            oSpecialVariableMap.Add("$MyPictures", Environment.GetFolderPath(Environment.SpecialFolder.MyPictures));
            oSpecialVariableMap.Add("$Recent", Environment.GetFolderPath(Environment.SpecialFolder.Recent));
            oSpecialVariableMap.Add("$SendTo", Environment.GetFolderPath(Environment.SpecialFolder.SendTo));
            oSpecialVariableMap.Add("$StartMenu", Environment.GetFolderPath(Environment.SpecialFolder.StartMenu));
            oSpecialVariableMap.Add("$Startup", Environment.GetFolderPath(Environment.SpecialFolder.Startup));
            oSpecialVariableMap.Add("$System", Environment.GetFolderPath(Environment.SpecialFolder.System));
            oSpecialVariableMap.Add("$Templates", Environment.GetFolderPath(Environment.SpecialFolder.Templates));
            oSpecialVariableMap.Add("$DesktopDirectory", Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory));
            oSpecialVariableMap.Add("$Personal", Environment.GetFolderPath(Environment.SpecialFolder.Personal));
            oSpecialVariableMap.Add("$MyDocuments", Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments));
            oSpecialVariableMap.Add("$ProgramFiles", Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles));
            oSpecialVariableMap.Add("$CommonProgramFiles", Environment.GetFolderPath(Environment.SpecialFolder.CommonProgramFiles));
        }

        public static string ResolveFolderOrFilePath(string sPath)
        {
            Regex rx = new Regex(@"\$(?<varName>[^\\]+)", RegexOptions.ExplicitCapture | RegexOptions.IgnoreCase | RegexOptions.Singleline);
            string sResult = sPath;
            
            MatchCollection matches = rx.Matches(sPath);
            if (matches != null && matches.Count > 0)
            {
                for (int i = 0; i < matches.Count; i++)
                {
                    if (matches[i].Groups["varName"] != null)
                    {
                        string sVariableName = "$" + matches[i].Groups["varName"].Value;
                        sResult = sResult.Replace(sVariableName, oSpecialVariableMap[sVariableName]);
                    }
                }
            }

            return sResult;
        }
    }
}
